// hyb_yl/mysubpages/pages/openservice/openservice.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    twcheck: false,
    timeLeft: '',
    zxcheck: false,
    timezx: '',
    service: [],
  },
  opening: function (e) {
    console.log(e);
    var that = this;
    const state = e.currentTarget.dataset.state;
    var id = e.currentTarget.dataset.id
    var title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: '/hyb_yl/backstageTeam/pages/serviceset/serviceset?state=' + state + '&id=' + id + '&title=' + title,
    })
    return false
  },
  open: function () {
    console.log("开通");
    return false
  },
  twchanges: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var stateback = e.currentTarget.dataset.stateback;
    var money = e.currentTarget.dataset.money;
    var gq = e.currentTarget.dataset.gq;
    if (stateback == '0') {
      //开启
      wx.showModal({
        title: '提示',
        content: "确认开启该服务吗？",
        success: function (con) {
          if (con.confirm) {
            if (!gq || money == '0' || money == '0.00' || money == '') {
              app.util.request({
                'url': 'entry/wxapp/team.update_switch',
                data: {
                  id: id,
                  stateback: 1
                },
                success: function (res) {
                  that.getServelist(that.data.id)
                }
              })
              
            }else{
              app.util.request({
                'url': 'entry/wxapp/team.server_pay',
                header: {
                  "Content-Type": "application/xml"
                },
                method: "GET",
                data: {
                  openid: wx.getStorageSync('openid'),
                  money: money,
                  id: id,
                  typs: 'team'
                },
                success: function (res) {
                  wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr, //字符串随机数
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    success: function (res) {
                      app.util.request({
                        'url': 'entry/wxapp/team.update_switch',
                        data: {
                          id: id,
                          stateback: 1
                        },
                        success: function (res) {
                          that.getServelist(that.data.id)
                        }
                      })
                    },
                    fail: function (res) {
                      console.log(res)
                    }
                  })
                }
              })
            }
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content:"确认关闭该服务吗？",
        success:function(con)
        {
          if(con.confirm)
          {
            app.util.request({
              'url': 'entry/wxapp/team.update_switch',
              data: {
                id: id,
                stateback: 0
              },
              success: function (res) {
                console.log(res)
                that.getServelist(that.data.id)
              }
            })
          }
        }
      })
      
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id
    that.setData({
      id: id
    })
    this.getServelist(id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getServelist(id) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/team.servertime',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          server: res.data
        })
      }
    })
  }

})